package cn.itheima.action.system;

import cn.itheima.action.common.BaseAction;
import cn.itheima.domain.system.User;
import cn.itheima.service.system.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.convention.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

/**
 * Created by millions on 2017/7/20.
 */
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class UserAction extends BaseAction<User> {


    @Action(value = "user_login",results =
            {@Result(type = "redirect",location = "index.html"),
            @Result(name="login",type = "redirect",location = "login.html")})
    public String login(){

        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken(model.getUsername(), model.getPassword());

        try {
            subject.login(token);
            return SUCCESS;
        } catch (AuthenticationException e) {
            e.printStackTrace();
        }

        return LOGIN;
    }

    @Action(value = "logout",results = {@Result(type = "redirect",location = "login.html")})
    public String logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return SUCCESS;
    }
}
